package net.yla.smartbus.module.controller.web.ba.dept;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.module.ba.dept.form.DeptForm;
import net.yla.smartbus.module.ba.dept.query.DeptEmpQuery;
import net.yla.smartbus.module.ba.dept.query.DeptQuery;
import net.yla.smartbus.module.ba.dept.service.DeptService;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/17 15:34
 */
@RestController
@RequestMapping("/api/web/dept")
public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private RolesService rolesService;

    /**
     * 查询部门信息
     * @param query
     * @return
     */
    @PostMapping("/query")
    public ApiResult listDeptInfo(@RequestBody DeptQuery query){
        rolesService.userRoleEvaluation(UserRoleEnum.DEPARTMENT_SELECT,getToken(),getIpAddress());
        return success(deptService.listDeptInfo(query));
    }

    /**
     * 删除部门信息
     * @param query
     * @return
     */
    @PostMapping("/delete")
    public ApiResult deleteDept(@RequestBody DeptQuery query){
        rolesService.userRoleEvaluation(UserRoleEnum.DEPARTMENT_DELETE,getToken(),getIpAddress());
        return success(deptService.deleteDept(query));
    }

    /**
     * 新增部门
     * @param form
     * @return
     */
    /*@PostMapping("/add")
    public ApiResult addDept(@RequestBody DeptForm form){
        rolesService.userRoleEvaluation(UserRoleEnum.DEPARTMENT_INSERT,getToken(),getIpAddress());
        return success(deptService.addDept(form));
    }*/

    /**
     *更新表单
     * @param form
     * @return
     */
    @PostMapping("/edit")
    public ApiResult update(@RequestBody DeptForm form){
        if (form.getDeptId() != null){
            rolesService.userRoleEvaluation(UserRoleEnum.DEPARTMENT_UPDATE,getToken(),getIpAddress());
        }else {
            rolesService.userRoleEvaluation(UserRoleEnum.DEPARTMENT_INSERT,getToken(),getIpAddress());
        }

        return success(deptService.update(form));
    }

    /**
     * 根据分组查询所有人员信息
     * @return
     */
    @PostMapping("/empInfo")
    public ApiResult listEmpInfo(@RequestBody DeptEmpQuery query){
        return success(deptService.listEmpInfo(query));
    }


    /**
     * 查询所有人员
     * @param query
     * @return
     */
    @PostMapping("/people")
    public ApiResult listHavePeople(@RequestBody DeptEmpQuery query){
        return success(deptService.listHavePeople(query.getDeptId()));
    }


}
